

/* 

**************************************************

##############  取消注释即可查看作业  ##############

########  因为分为多个文件可能不方便检查作业  ########

###### 快捷键：alt + shift + A  取消多行注释  ######

***************************************************

*/

/* 
    **================================================**
    练习一：
*/
/* let ageArr = [];
let count = 1;//循环计数
let cycles = 5;//循环限定次数
for (; ;) { 
    const num = prompt(`请输入第${ageArr.length+1}个有效年龄[0~100]:`);
    if (isNaN(num)||num === "") {
        alert(isNaN(+num)?`[无效的输入]\n原因：非数字\n请重新输入！`:`[无效的输入]\n原因：未输入\n请重新输入！`);
    } else if (num > 100 || num < 0) {
        alert(num<0?"年龄区间错误，请重新输入！\n原因：年龄过小！":"年龄区间错误，请重新输入！\n原因：年龄过大！");
    } else { 
        ageArr.push(+num);
        if (count++ === cycles) break;
    }
}

let str = "";
let sum = [0,0,100];
// for (let i in ageArr) { //可选方式：增强for
for (let i = 0;i < cycles;i++) {
    str += ageArr[i] >= 18 ? ageArr[i] + " " : '';
    sum[0] += ageArr[i];
    ageArr[i] > sum[1]? sum[1] = ageArr[i]:''
    ageArr[i] < sum[2]? sum[2] = ageArr[i]:''
}
document.write(str===""?`<p style="color:red;">没有成年人年龄！</p>`:`<p">成年人的年龄有：${str}</p>`);
document.write(`<p>年龄总和为：${sum[0]}</p>`);
document.write(`<p>平均年龄为：${sum[0]/cycles}</p>`);
document.write(`<p>最大年龄为：${sum[1]}</p>`);
document.write(`<p>最小年龄为：${sum[2]}</p>`); */

/* 
    **================================================**
    练习二：
*/

/* let mark = 10;//标记的数字
let numArr = [88, 20, 10, 100, 50, 88, 20, 10, 100, 50, 88, 20, 10, 100, 50];
let haveNum = "";
for (let i = 0;i < numArr.length;i++) {
    haveNum += numArr[i] === mark ? i+' ' : '';
}
document.write(haveNum === ""?`-1 [未找到数字为${mark}的元素]`:`数字为${mark}的元素角标有：${haveNum}`);
 */
/* 
    **================================================**
    练习三：
*/

/* let numArr = [5, 8, 9, 2, 1, 5];
let sum = 0;
for (let i = 0;i < numArr.length;i++) {
    sum += numArr[i];
}
console.log(`总和为：${sum}`); */

/* 
    **================================================**
    练习四：
*/

/* let numArr = [4, 9, 5, 20, 3, 11];
let sum = 0;
let sumI = 0;
for (let i = 0;i < numArr.length;i++) {
    numArr[i]>5?(sum += numArr[i])&&(sumI += i):'';
}
console.log(`大于5的数的和为：${sum}`); 
console.log(`大于5的i的和为：${sumI}`);
console.log(`【这题的i是真陷阱啊，差点就G了】`); */

/* 
    **================================================**
    练习五：
*/

/* let ageArr = [15, 19, 21, 33, 18, 24];
let sum = 0;
for (let i = 0;i < ageArr.length;i++) {
    sum += ageArr[i];
}
console.log(`平均数为：${sum / ageArr.length}`);
 */

/* 
    **================================================**
    练习六：
*/

/* let numArr = [2, 6, 18, 15, 40];
let sum = 0;
for (let i = 0; i < numArr.length; i++) {
    numArr[i] % 6? sum += numArr[i] : '';
}
console.log(`被3整除的偶数的和为：${sum}`); */

/* 
    **================================================**
    练习七：
*/

/* let numArr = [2, 6, 18, 15, 40];
let count = 0;
for (let i = 0; i < numArr.length; i++) {
    numArr[i] % 6 ? count++ : '';
}
console.log(`被3整除的偶数的个数为：${count}个`); */

/* 
    **================================================**
    练习八：
*/
/* 
let Arr = [2, 6, 18, 15, 40];
let newArr = [];
for (let i = 0; i < Arr.length; i++) {
    Arr[i] ? newArr.push(Arr[i]) : '';
}
// console.log(newArr); */